package com.bgds.sys.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bgds.sys.module.entity.dto.DeviceDto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author lj
 * 状态常量
 * 树工具类
 */
public class TreeUtils {

    /**
     * list 转JSONArray
     * @param list
     * @return
     */
    public static JSONArray listToJsonArray(List<?> list){
        return  JSONArray.parseArray(JSON.toJSONString(list));
    }

    /**
     * 构建菜单树
     * @param menuList
     * @param parentCode
     * @return
     */
    public static JSONArray treeList(JSONArray menuList, String parentCode) {
        JSONArray childMenu = new JSONArray();
        for (Object object : menuList) {
            JSONObject jsonMenu = JSONObject.parseObject(JSONObject.toJSONString(object));
            String deviceCode = String.valueOf(jsonMenu.get("deviceCode"));
            String pc =String.valueOf(jsonMenu.get("parentCode"));
            if (parentCode.equals(pc) ) {
                JSONArray jsonArray = treeList(menuList, deviceCode);
                jsonMenu.put("children", jsonArray);
                childMenu.add(jsonMenu);
            }
        }
        return childMenu;
        }
    /**
     * 第三方监测点构建菜单树
     * @param menuList
     * @param parentCode
     * @return
     */
    public static JSONArray treeToList(JSONArray menuList, String parentCode) {
        JSONArray childMenu = new JSONArray();
        for (Object object : menuList) {
            JSONObject jsonMenu = JSONObject.parseObject(JSONObject.toJSONString(object));
            String deviceCode = String.valueOf(jsonMenu.get("id"));
            String pc =String.valueOf(jsonMenu.get("pid"));
            if (parentCode.equals(pc) ) {
                JSONArray jsonArray = treeList(menuList, deviceCode);
                jsonMenu.put("measurementPoints", jsonArray);
                childMenu.add(jsonMenu);
            }
        }
        return childMenu;
    }

    public static List<DeviceDto> listToTree(List<DeviceDto> oldList){
        Map<String,Object> newMap = new HashMap<>(10);
        List<DeviceDto> newList = new ArrayList<>();
        for (DeviceDto treeDto : oldList){
            newMap.put(treeDto.getDeviceCode(),treeDto);
        }

        for (DeviceDto treeDto : oldList){
            DeviceDto parent = (DeviceDto) newMap.get(treeDto.getParentCode());
            if (parent != null){
                if (parent.getChildren() == null){
                    List<DeviceDto> ch = new ArrayList<>();
                    ch.add(treeDto);
                    parent.setChildren(ch);
                }else {
                    List<DeviceDto> ch = parent.getChildren();
                    ch.add(treeDto);
                    parent.setChildren(ch);
                }
            }else {
                newList.add(treeDto);
            }
        }
        return newList;
    }
}
